package cc.rengu.igas.bomp.core.service.trans;


import cc.rengu.igas.bomp.common.dao.TxnLimitCfgMapper;
import cc.rengu.igas.bomp.common.dao.impl.TxnLimitCfgMapperImpl;
import cc.rengu.igas.bomp.common.entity.TxnLimitCfg;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.TxnLimitCfgBean;
import cc.rengu.igas.bomp.facade.request.UserTxnLimitCfgQueryRequest;
import cc.rengu.igas.bomp.facade.response.UserTxnLimitCfgQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.enums.LimitTypeEnum;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户限额配置查询
 * Created by wangcheng
 */
public class UserTxnLimitCfgQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        UserTxnLimitCfgQueryRequest userTxnLimitCfgQueryRequest = new UserTxnLimitCfgQueryRequest();
        ConvertUtil.convertOutput(userTxnLimitCfgQueryRequest);
        return userTxnLimitCfgQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        UserTxnLimitCfgQueryRequest userTxnLimitCfgQueryRequest = (UserTxnLimitCfgQueryRequest) request;
        UserTxnLimitCfgQueryResponse userTxnLimitCfgQueryResponse = new UserTxnLimitCfgQueryResponse();
        BizResponse<UserTxnLimitCfgQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.IGAS);
        BeanUtil.beanCopy(userTxnLimitCfgQueryRequest, userTxnLimitCfgQueryResponse);

        String instId = userTxnLimitCfgQueryRequest.getHeader().getInstId();
        String userId = userTxnLimitCfgQueryRequest.getUserId();
        String userInst = userTxnLimitCfgQueryRequest.getUserInst();

        String limitType;
        String limitInst;
        List<TxnLimitCfg> limitCfgs;
        TxnLimitCfgMapper txnLimitCfgMapper = new TxnLimitCfgMapperImpl();
        List<TxnLimitCfgBean> b2cLimitList = new ArrayList<>();        /* B2C限额配置 */
        if (!StringUtil.isEmptyOrNull(userId)) {
            limitType = LimitTypeEnum.B2C_STATIC.getLimitType();
            limitInst = userId;
            limitCfgs = txnLimitCfgMapper.selectTxnLimitCfgLimitTypeInst(instId, limitType, limitInst);
            if (null != limitCfgs && !limitCfgs.isEmpty()) {
                convTxnLimitCfg(limitCfgs, b2cLimitList);
            }
        }

        List<TxnLimitCfgBean> c2bLimitList = new ArrayList<>();         /* C2B限额配置 */
        if (!StringUtil.isEmptyOrNull(userId)) {
            limitType = LimitTypeEnum.C2B_QR.getLimitType();
            limitInst = userId;
            limitCfgs = txnLimitCfgMapper.selectTxnLimitCfgLimitTypeInst(instId, limitType, limitInst);
            if (null != limitCfgs && !limitCfgs.isEmpty()) {
                convTxnLimitCfg(limitCfgs, c2bLimitList);
            }
        }

        List<TxnLimitCfgBean> cashierLimitList = new ArrayList<>();        /* 收银台限额配置 */
        if (!StringUtil.isEmptyOrNull(userInst) && !StringUtil.isEmptyOrNull(userId)) {
            limitType = LimitTypeEnum.CASHIER.getLimitType();
            limitInst = userInst + "|" + userId;
            limitCfgs = txnLimitCfgMapper.selectTxnLimitCfgLimitTypeInst(instId, limitType, limitInst);
            if (null != limitCfgs && !limitCfgs.isEmpty()) {
                convTxnLimitCfg(limitCfgs, cashierLimitList);
            }
        }

        userTxnLimitCfgQueryResponse.setB2cLimitList(b2cLimitList);
        userTxnLimitCfgQueryResponse.setC2bLimitList(c2bLimitList);
        userTxnLimitCfgQueryResponse.setCashierLimitList(cashierLimitList);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(userTxnLimitCfgQueryResponse);

        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        UserTxnLimitCfgQueryResponse userTxnLimitCfgQueryResponse = (UserTxnLimitCfgQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(userTxnLimitCfgQueryResponse);
    }


    private void convTxnLimitCfg(List<TxnLimitCfg> limitCfgs, List<TxnLimitCfgBean> transLimitList) {
        for (TxnLimitCfg limitCfg : limitCfgs) {
            TxnLimitCfgBean bean = new TxnLimitCfgBean();
            BeanUtil.beanCopy(limitCfg, bean);
            transLimitList.add(bean);
        }
    }
}
